<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加管家</title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <!-- 替换为CDN引入的Layui CSS -->
    <link href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css" media="all" rel="stylesheet">
    <style>
        .layui-form-item .layui-input-inline {
            width: 380px;
        }

        .layui-form-label {
            width: 120px;
        }

        .x-red {
            color: red;
            margin-right: 4px;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">业主信息</div>
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="formDemo">
                <!--          <div class="layui-form-item">
                              <label class="layui-form-label"><span class="x-red">*</span>选择房产</label>
                              <div class="layui-input-block">
                                  <select lay-search lay-verify="required" name="houseId">
                                      <option value="">请选择房产</option>
                                  </select>
                              </div>
                          </div>-->

                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="x-red">*</span>业主姓名</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="name"
                               placeholder="请输入姓名"
                               required type="text">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="x-red">*</span>联系电话</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required|phone" name="phone"
                               placeholder="请输入联系电话" required type="text">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="x-red">*</span>密码</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="required" name="password"
                               placeholder="请输入密码" required type="text">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-filter="formDemo" lay-submit>立即提交</button>
                        <button class="layui-btn layui-btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
<script>
    // 删除原有的layui.js引用
    layui.use(['form', 'jquery', 'layer'], function () {
        var form = layui.form;
        var $ = layui.$; // 注意这里改为layui.$
        var layer = layui.layer;

        // 加载管家管辖的房产
        // $.ajax({
        //     url: '/manager/properties',
        //     data: {managementId: sessionStorage.getItem("userId")}, // 这里应该是当前登录管家的ID
        //     beforeSend: function () {
        //         layer.load(1);
        //     },
        //     success: function (res) {
        //         layer.closeAll('loading');
        //         var html = '<option value="">请选择房产</option>';
        //         $.each(res.data, function (index, item) {
        //             html += '<option value="' + item.propertyId + '">' + item.buildingNumber + '-' + item.unitNumber + '-' + item.roomNumber + '室 ' + (item.status == 1 ? '已售出' : '') + '</option>';
        //         });
        //         $('select[name="houseId"]').html(html);
        //         form.render('select');
        //     },
        //     error: function () {
        //         layer.closeAll('loading');
        //         layer.msg('加载房产数据失败', {icon: 5});
        //     }
        // });

        // 自定义验证规则
        form.verify({
            phone: [/^1[3-9]\d{9}$/, '请输入正确的手机号码'],
            // identity: [/^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/, '请输入正确的身份证号']
        });

        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return false;
        }

        var orderId = getQueryVariable('id');
        var orderType = getQueryVariable('type');
        if (orderType == "manager") {
            //     添加管家
            form.on('submit(formDemo)', function (data) {
                var loadIndex = layer.load(1);
                console.log(data.field)
                // 新增业主请求
                $.ajax({
                    url: '/admin/stuff/addManage',
                    type: 'post',
                    data: {
                        userName: data.field.name,
                        phone: data.field.phone,
                        password: data.field.password,
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg(res.msg, {icon: 1});
                            setTimeout(function () {
                                parent.layer.closeAll();
                                parent.loadComplaints();
                            }, 1000);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                    error: function () {
                        layer.close(loadIndex);
                        layer.msg('管家添加失败', {icon: 5, time: 2000});
                    }
                });
                return false;
            });
        } else {
            //     添加员工
            form.on('submit(formDemo)', function (data) {
                var loadIndex = layer.load(1);
                console.log(data.field)
                // 新增业主请求
                $.ajax({
                    url: '/admin/stuff/addStuff',
                    type: 'post',
                    data: {
                        userName: data.field.name,
                        phone: data.field.phone,
                        password: data.field.password,
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg(res.msg, {icon: 1});
                            setTimeout(function () {
                                parent.layer.closeAll();
                                parent.loadComplaints();
                            }, 1000);
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                    error: function () {
                        layer.close(loadIndex);
                        layer.msg('管家添加失败', {icon: 5, time: 2000});
                    }
                });
                return false;
            });
        }
        // 监听提交

    });
</script>
</body>
</html>
